/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCR;

import Enums.ProjectType;
import graphics.GraphicsEngin;
import graphics.BrightnessIncrease;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.image.BufferedImage;
import javax.media.jai.JAI;
import javax.swing.JButton;
import javax.swing.JSlider;
import popups.PopupFrame;
import popups.PopupFrameManager;

/**
 *
 * @author marwa
 */
public class Toolbox extends javax.swing.JInternalFrame {

    PopupFrameManager popupDisplayer;
    PopupFrame popUp = new PopupFrame();

    /**
     * Creates new form Toolbox
     */
    public Toolbox() {
        initComponents();

        popupDisplayer = new PopupFrameManager();
        GridLayout gLayout = new GridLayout(4, 3);
        popUp.getContainer().setLayout(gLayout);
    }

    public void setToolsEnabled(boolean ena) {
        this.jButton1.setEnabled(ena);
        this.jButton2.setEnabled(ena);
        this.jButton3.setEnabled(ena);
        this.jButton4.setEnabled(ena);
        this.opacity.setEnabled(ena);
        this.brightness.setEnabled(ena);
    }

    public void initializeTools(ProjectType projectType) {

        if (projectType.equals(ProjectType.PCR)) {
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
            jButton4.setEnabled(false);

        } else if (projectType.equals(ProjectType.MRNA)) {
            jButton1.setEnabled(true);
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);
            jButton4.setEnabled(true);

        } else if (projectType.equals(ProjectType.PRO)) {
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            jButton3.setEnabled(false);
            jButton4.setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        opacity = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        brightness = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Toolbox");

        opacity.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                opacityStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        jLabel1.setText("Background Opacity");

        jButton1.setText("Bands");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Smears");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Dimers");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        brightness.setMinimum(20);
        brightness.setValue(60);
        brightness.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                brightnessStateChanged(evt);
            }
        });

        jLabel2.setText("Image Brightness");

        jButton4.setText("House Keeping");
        jButton4.setEnabled(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(opacity, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(brightness, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(27, 27, 27))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(brightness, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(opacity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48))
        );

        jButton1.getAccessibleContext().setAccessibleName("Band");

        getAccessibleContext().setAccessibleName("Band");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void opacityStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_opacityStateChanged
        JSlider source = (JSlider) evt.getSource();
        if (!source.getValueIsAdjusting()) {
            int opacityValue = (int) source.getValue();
            MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
            frm.getSelectedWorkArea().setSaved(false);
            GraphicsEngin.changeBackground(frm.getSelectedWorkArea().getBandsPanel(), opacityValue);
        }
    }//GEN-LAST:event_opacityStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JButton curBtn = (JButton) evt.getSource();
        PointerInfo a = MouseInfo.getPointerInfo();
        Point pt = a.getLocation();
        popupDisplayer.getBands(popUp.getContainer(), false);
        popUp.setLocation(pt.x, pt.y);
        popUp.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        JButton curBtn = (JButton) evt.getSource();
        PointerInfo a = MouseInfo.getPointerInfo();
        Point pt = a.getLocation();
        popupDisplayer.getSmears(popUp.getContainer(), false);
        popUp.setLocation(pt.x, pt.y);
        popUp.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        JButton curBtn = (JButton) evt.getSource();
        PointerInfo a = MouseInfo.getPointerInfo();
        Point pt = a.getLocation();
        popupDisplayer.getDimers(popUp.getContainer(), false);
        popUp.setLocation(pt.x, pt.y);
        popUp.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void brightnessStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_brightnessStateChanged
        // TODO add your handling code here:
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.getSelectedWorkArea().setSaved(false);
        if (frm.getSeletedShape() != null) {
            BufferedImage image = JAI.create("fileload", frm.getSeletedShape().getName()).getAsBufferedImage();
            float value = (float) brightness.getValue();
            float scaleFactor = 2 * value / brightness.getMaximum();
            BrightnessIncrease.imageBrightness(scaleFactor, frm.getSeletedShape(), image);
        }
    }//GEN-LAST:event_brightnessStateChanged

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        JButton curBtn = (JButton) evt.getSource();
        PointerInfo a = MouseInfo.getPointerInfo();
        Point pt = a.getLocation();
        popupDisplayer.getHouseKeepings(popUp.getContainer(), false);
        popUp.setLocation(pt.x, pt.y);
        popUp.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider brightness;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSlider opacity;
    // End of variables declaration//GEN-END:variables
}
